/*
 * main.cc
 *
 *  Created on: Dec 9, 2010
 *      Author: fify
 */

/*
 * Note: there may be multiple pipes from the same source and sink.
 */
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

const int MAX_VER = 1000;

int cap[MAX_VER][MAX_VER];

void init_graph()
{
	memset(cap, 0, sizeof(cap));
}

int queue[MAX_VER];
int flag[MAX_VER];
int head, tail;

// Path.
int pre[MAX_VER];
// We can use BFS, DFS, Random search and so on.
int search_path(int s, int t, int n)
{
	memset(flag, 0, sizeof(flag));
	head = tail = 0;

	queue[tail ++] = s;
	flag[s] = 1;

	int push_flow = 0x7fffffff;

	while(tail > head)
	{
		int now = queue[head++];
		for(int i = 0;i<n;i++)
		{
			if(!flag[i] && cap[now][i] > 0)
			{
				push_flow = min(push_flow, cap[now][i]);

				pre[i] = now;
				queue[tail++] = i;

				flag[i] = 1;

				if(i == t)
					return push_flow;
			}
		}
	}

	return 0;
}

int max_flow(int s, int t, int n)
{
	int ret = 0;
	int flow;
	int vs, vt = t;
	while((flow = search_path(s, t, n)))
	{
		ret += flow;

		vt = t;

		while(1)
		{
			vs = pre[vt];
			//cout << vs << " " << vt << endl;
			cap[vs][vt] -= flow;
			cap[vt][vs] += flow;
			if(vs == s)
				break;
			vt = pre[vt];
		}
	}

	return ret;
}

int main()
{
	freopen("data.in", "r", stdin);
	int m, n;	// n: vertex count, m: edge number.
	int s, t, c;

	while(cin >> m >> n)
	{
		init_graph();

		for(int i = 0;i<m;i++)
		{
			cin >> s >> t >> c;
			cap[s][t] += c;
		}

		cout << max_flow(1, n, n + 1) << endl;
	}

	return 0;
}
